package com.eu.ebpm.engine.cmd;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.eu.ebpm.engine.entity.args.QueryParam;
import com.eu.ebpm.engine.query.Page;
import com.eu.ebpm.engine.repository.TaskRepository;
import com.eu.ebpm.engine.service.Task;
import com.eu.mybatis.helper.RowsHelper;

/**
 * 查询运行的任务列表
 * @author 王晓华
 */
@Component
public class TaskListCommand implements Command<Object> {

	@Autowired
	private TaskRepository taskRepository;
	
	public Object execute(QueryParam queryParam) {
		
		RowBounds rowBounds = RowBounds.DEFAULT;
		
		if(queryParam.isPage()) {
			rowBounds = new RowBounds((queryParam.getPageNum() - 1) * queryParam.getPageSize(), queryParam.getPageSize());
		}
		
		List<Task> taskList = null;
		if(queryParam.isIdentity()) {
			taskList = taskRepository.taskListByIdentity(queryParam.getMap(), rowBounds);
		} else {
			taskList = taskRepository.taskList(queryParam.getMap(), rowBounds);
		}
		
		if(queryParam.isPage()) {
			return new Page<Task>(RowsHelper.getTotalRows(), taskList);
		}
		
		return taskList;
	}

}
